package com.example.myapplication.utils;

import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Build;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import java.util.ArrayList;
import java.util.List;

public class PermissionUtils extends AppCompatActivity {
    private static PermissionUtils permissionUtils;
    private final int mRequestCode = 20;

    private OnPermissionCallbackListener listener;//声明接口

    private PermissionUtils() {
    }

    public static PermissionUtils getInstance() {
        if (permissionUtils == null) {
            synchronized (PermissionUtils.class) {
                if (permissionUtils == null) {
                    permissionUtils = new PermissionUtils();
                }
            }
        }
        return permissionUtils;
    }

    public void requestPermission(Activity context, String[] permission, OnPermissionCallbackListener listener) {
        //判断手机版本，6.0以上需要申请权限
        this.listener = listener;
        if (Build.VERSION.SDK_INT >= 23) {
            //创建一个集合，将之前用户没有授予的权限放到集合之中统一管理
            List<String> mPermissionList = new ArrayList<String>();
            //逐个判断权限是否授权
            for (String s : permission) {
                int res = ContextCompat.checkSelfPermission(context, s);
                if (res != PackageManager.PERMISSION_GRANTED) {
                    mPermissionList.add(s);
                }
            }
            //申请权限
            if (!mPermissionList.isEmpty()) {
                ActivityCompat.requestPermissions(context, mPermissionList.toArray(new String[mPermissionList.size()]), mRequestCode);
            } else {
                //权限都要通过了
                listener.onGranted();
            }
        }
    }

    public void onRequestPermissionsResult(Activity context, int requestCode,
                                           @NonNull String[] permissions,
                                           @NonNull int[] grantResults) {
        List<String> deniedPermission = new ArrayList<>();
        if (requestCode == mRequestCode) {
            if (grantResults.length > 0) {
                for (int i = 0; i < grantResults.length; i++) {
                    if (grantResults[i] != PackageManager.PERMISSION_GRANTED) {
                        deniedPermission.add(permissions[i]);
                    }
                }
            }
            if (deniedPermission.isEmpty()){
                listener.onGranted();
            }else {
                listener.onDenied(deniedPermission);
            }
        }else {
            listener.onGranted();
        }

    }
    /**
     * 提示用户前往应用设置页面手动开启权限
     *
     */
    public void showDialogUserGotoSetting(Activity context){
        DialogUtils.showNormalDialog(context, "提示信息", "权限已经禁用，请手动开启", "取消",
                new DialogUtils.OnLeftClickListener() {
                    @Override
                    public void onLeftClick() {
                        context.finish();
                    }
                }, "确定", new DialogUtils.OnRightClickListener() {
                    @Override
                    public void onRightClick() {
                        JumpSysPageUtils.goToAppSetting(context);
                        context.finish();
                    }
                });
    }
}
